package cn.acewill.yxd.yunxundian.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

import java.text.DecimalFormat;

/**
 * Created by XXX on 2017/12/21.
 */

public class UpdateUtils {

    /**
     * 检查是否存在SDCard
     *
     * @return
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 2 * 获取版本号 3 * @return 当前应用的版本号 4
     */
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            String version = info.versionName;
           // int versioncode = info.versionCode;
          //  return versioncode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static int getVersionCode(Context context){
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1000;
    }

    /**
     * 流量计算(kb)
     */
    public static String caluateSize(int size){
        DecimalFormat formater = new DecimalFormat("####.0");//保三
        if (size>1024){
            float tsize = size/1024f;
            return formater.format(tsize)+"M";
        }else {
            return size + "kb";
        }
    }



}
